package com.dqhl.smartstable.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dqhl.smartstable.R;
import com.dqhl.smartstable.base.BaseActivity;
import com.dqhl.smartstable.model.Contacts;
import com.dqhl.smartstable.util.Config;
import com.dqhl.smartstable.util.Constant;
import com.dqhl.smartstable.util.Dlog;
import com.dqhl.smartstable.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by mac on 16/10/18.
 * 紧急联系人
 */
public class ContactsActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_top_back;
    private TextView tv_top_center;
    private TextView tv_top_right;

    private EditText et_name_one;
    private EditText et_phone_one;
    private EditText et_name_two;
    private EditText et_phone_two;

    private boolean isEditable = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts);

        initView();
        initData();
    }

    private void initData() {
        RequestParams params = new RequestParams(Config.Api.contacts);
        params.addBodyParameter("token", sp.getString(Constant.TOKEN, ""));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                String msg = JsonUtils.getMsg(result);
                String code = JsonUtils.getCode(result);
                String data = JsonUtils.getData(result);
                if ("2000".equals(msg)) {
                    Contacts contacts = JSON.parseObject(data, Contacts.class);
                    et_name_one.setText(contacts.getOne_relation_name());
                    et_phone_one.setText(contacts.getOne_relation_phone());
                    et_name_two.setText(contacts.getTwo_relation_name());
                    et_phone_two.setText(contacts.getTwo_relation_phone());
                } else {
                    toast(code);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initView() {
        iv_top_back = (ImageView) findViewById(R.id.iv_top_back);
        tv_top_center = (TextView) findViewById(R.id.tv_top_center);
        tv_top_center.setText("紧急联系人");
        tv_top_right = (TextView) findViewById(R.id.tv_top_right);
        if (isEditable) {
            tv_top_right.setText("保存");
        } else {
            tv_top_right.setText("编辑");
        }

        et_name_one = (EditText) findViewById(R.id.et_name_one);
        et_phone_one = (EditText) findViewById(R.id.et_phone_one);
        et_name_two = (EditText) findViewById(R.id.et_name_two);
        et_phone_two = (EditText) findViewById(R.id.et_phone_two);

        iv_top_back.setOnClickListener(this);
        tv_top_right.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
            case R.id.tv_top_right: //编辑
                if (isEditable) {
                    editContactsInfo();
                } else {
                    tv_top_right.setText("保存");
                    et_phone_two.setFocusable(true);
                    et_phone_two.setFocusableInTouchMode(true);
                    et_phone_two.requestFocus();
                    et_name_two.setFocusable(true);
                    et_name_two.setFocusableInTouchMode(true);
                    et_name_two.requestFocus();
                    et_phone_one.setFocusable(true);
                    et_phone_one.setFocusableInTouchMode(true);
                    et_phone_one.requestFocus();
                    et_name_one.setFocusable(true);
                    et_name_one.setFocusableInTouchMode(true);
                    et_name_one.requestFocus();
                    isEditable = true;
                }
                break;
        }
    }

    private void editContactsInfo() {
        String strNameOne = et_name_one.getText().toString().trim();
        String strPhoneOne = et_phone_one.getText().toString().trim();
        String strNameTwo = et_name_two.getText().toString().trim();
        String strPhoneTwo = et_phone_two.getText().toString().trim();
        RequestParams params = new RequestParams(Config.Api.edit_contacts);
        params.addBodyParameter("token", sp.getString(Constant.TOKEN, ""));
        params.addBodyParameter("one_relation_name", strNameOne);
        params.addBodyParameter("one_relation_phone", strPhoneOne);
        params.addBodyParameter("two_relation_name", strNameTwo);
        params.addBodyParameter("two_relation_phone", strPhoneTwo);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                String msg = JsonUtils.getMsg(result);
                String code = JsonUtils.getCode(result);
                String data = JsonUtils.getData(result);
                if ("2000".equals(msg)){
                    tv_top_right.setText("编辑");
                    isEditable = false;
                    et_name_one.setFocusable(false);
                    et_phone_one.setFocusable(false);
                    et_name_two.setFocusable(false);
                    et_phone_two.setFocusable(false);
                }
                toast(code);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
